﻿using System;
using RoMBot.MemoryLib;

namespace RoMBot.Classes
{
    public class Cursor
    {
        static Cursor()
        {
            Address = 0;
            ItemLocation = 0;
            ItemLocationName = "";
            ItemId = 0;
            ItemBagId = 0;

            Update();
        }

        public static void Update()
        {
            Address = ( int ) Memory.ReadUInt( Addresses.cursorBase );

            ItemLocation = Memory.ReadInt( Address + Addresses.cursorItemLocation_offset );
            switch ( ItemLocation )
            {
                case 0:
                    ItemLocationName = "<EMPTY>";
                    break;
                case 1:
                    ItemLocationName = "inventory";
                    break;
                case 3:
                    ItemLocationName = "bank";
                    break;
                case 4:
                    ItemLocationName = "equipment";
                    break;
                case 6:
                    ItemLocationName = "actionbar";
                    break;
                case 8:
                    ItemLocationName = "store";
                    break;
                case 14:
                    ItemLocationName = "guildbank";
                    break;
                default:
                    ItemLocationName = "other";
                    break;
            }

            ItemId = Memory.ReadInt( Address + Addresses.cursorItemId_offset );
            ItemBagId = Memory.ReadInt( Address + Addresses.cursorItemBagId_offset ) + 1;
        }

        public static bool HasItem
        {
            get
            {
                Update();
                return ItemLocation != 0;
            }
        }

        public static void Clear()
        {
            Update();

            var pickupmethod = "";
            switch ( ItemLocation )
            {
                case 0:
                    return;
                case 1:
                    pickupmethod = "PickupBagItem";
                    break;
                case 3:
                    pickupmethod = "PickupBankItem";
                    break;
                case 4:
                    pickupmethod = "PickupEquipmentItem";
                    break;
                case 14:
                    pickupmethod = "GuildBank_PickupItem";
                    break;
                default:
                    throw new Exception( "Unable to clear the cursor. Cursor item location ! supported." );
            }

            Helpers.RoMScript( string.Format( "{0}({1});", pickupmethod, ItemBagId ) );

            if ( HasItem )
            {
                throw new Exception( "Unable to clear the cursor. Returning the item failed." );
            }
        }

        public static int ItemBagId { get; set; }

        public static int ItemId { get; set; }

        public static string ItemLocationName { get; set; }

        public static int ItemLocation { get; set; }

        public static int Address { get; set; }
    }
}
